#include "im.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


void Usage(void)
{
  printf("ImInfo <input file>\n");
  printf("   Display file and image information for BMP, PCX, TIFF, GIF, JPEG, TGA, LED, RAS and SGI formats.\n");
  printf("<input file> - Image File Name\n");
}

void ErrorMsg(const char* Func, int err)
{
  char Text[256];
  strcpy(Text, Func);
  strcat(Text, " - ");
  
  switch (err)
  {
  case IM_ERR_OPEN:
    strcat(Text, "Error Opening File.\n");
    break;
  case IM_ERR_READ:
    strcat(Text, "Error Reading File.\n");
    break;
  case IM_ERR_WRITE:
    strcat(Text, "Error Writing File.\n");
    break;
  case IM_ERR_TYPE:
    strcat(Text, "Image not Suported.\n");
    break;
  case IM_ERR_FORMAT:
    strcat(Text, "Invalid Format.\n");
    break;
  case IM_ERR_COMP:
    strcat(Text, "Compression not suported.\n");
    break;
  default:
    strcat(Text, "Unknown Error.\n");
  }
  
  printf(Text);
}


void PrintFileInfo(const char* file, int f)
{
  char Text[256] = "";
  
  fprintf(stderr, "File Name: %s\n\n", file);
  
  switch ((unsigned char)f)
  {
  case IM_BMP:
    strcpy(Text, "Windows Bitmap");
    break;
  case IM_PCX:
    strcpy(Text, "PC PaintBrush");
    break;
  case IM_GIF:
    strcpy(Text, "Compuserve GIF");
    break;
  case IM_TIF:
    strcpy(Text, "TIFF - Tagged Image File Format");
    break;
  case IM_RAS:
    strcpy(Text, "Sun Raster File");
    break;
  case IM_SGI:
    strcpy(Text, "Silicon Graphics");
    break;
  case IM_TGA:
    strcpy(Text, "TrueVision Targa");
    break;
  case IM_JPG:
    strcpy(Text, "JPEG");
    break;
  case IM_LED:
    strcpy(Text, "Linguagem de Especificacao de Dialogos");
    break;
  }
  
  fprintf(stderr, "File Format: %s\n", Text);
  
  if (f >> 8)
    fprintf(stderr, "             Compressed\n");
  else
    fprintf(stderr, "             NOT Compressed\n");
}


void PrintImageInfo(int w, int h, int t, int pal_size)
{
  char Text[256] = "";
  
  fprintf(stderr, "\nImage Attributes\n");
  fprintf(stderr, "                Width:  %d\n", w);
  fprintf(stderr, "                Height: %d\n", h);
  
  switch (t)
  {
  case 0:
    strcpy(Text, "True Color RGB");
    break;
  default:
    sprintf(Text, "Indexed image %d colors", pal_size);
  }
  
  fprintf(stderr, "                Depth:  %s\n", Text);
}

int PrintRes(char *filename, double* xres, double* yres, int* res_unit)
{
  char* unit;

  switch(*res_unit)
  {
  case IM_RES_DPI:
    unit = "dpi";
    break;
  case IM_RES_DPC:
    unit = "dpc";
    break;
  default:
    return 1;
  }
  
  fprintf(stderr, "                X Res:  %g %s\n", *xres, unit);
  fprintf(stderr, "                Y Res:  %g %s\n", *yres, unit);

  return 1;
}

int main(int NumParam, char* ParamStr[])
{
  int w,h,t,f,err,pal_size;
  
  if (NumParam < 2 || ParamStr[1][0] == '?')
  {
    Usage();
    return 0;
  }
  
  printf("ImInfo %s\n", ParamStr[1]);
  
  err = imFileFormat(ParamStr[1], &f);
  
  if (err)
  {
    ErrorMsg("File Format", err);
    return 0;
  }
  
  PrintFileInfo(ParamStr[1], f);
  
  err = imImageInfo(ParamStr[1], &w, &h, &t, &pal_size);
  
  if (err)
  {
    ErrorMsg("Image Info", err);
    return 0;
  }
  
  PrintImageInfo(w, h, t, pal_size);

  imRegisterCallback((imCallback)PrintRes, IM_RESOLUTION_CB, IM_ALL);

  if (t == IM_RGB)
  {
    int size = w*h;
    unsigned char *red = malloc(size), *green = malloc(size), *blue = malloc(size);
    imLoadRGB(ParamStr[1], red, green, blue);
    free(red); free(green); free(blue);
  }
  else
  {
    int size = w*h;
    unsigned char *map = malloc(size);
    long color[256];
    imLoadMap(ParamStr[1], map, color);
    free(map);
  }

  
  return 1;
}
